package cn.zzh.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * 1.获取资源文件@PropertySource
 * 2.@Value注入资源文件中的信息
 * 3.调取DruidDataSource调用参数
 * 4.将获取得到的参数传到JdbcTemplate中
 */

@Configuration//把此类作为配置文件
@ComponentScan("cn.zzh")//扫描路径
@PropertySource("classpath:jdbc.properties")
public class SpringApplicationContextConfig{
    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DruidDataSource createDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }
    @Bean
    @Autowired
    public JdbcTemplate jdbcTemplate(DruidDataSource druidDataSource ){
        return new JdbcTemplate(druidDataSource);

    }




}
